﻿using DevExpress.Utils;
using DevExpress.XtraBars;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AHCS
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            //  Hanhua();
            //zh-Hans
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CN");
        }

        private void richEditControl1_DocumentLoaded(object sender, EventArgs e)
        {
          //  doc_default2(richEditControl1.Document);
     
           // richEditControl1.ActiveViewType= PrintLayout


        }


        //文档默认格式设置
        private void doc_default2(Document doc)
        {

            //页眉设置
            // 第一页 页眉设置
            //DocumentRange range1 = richEditControl1.Document.Range;
            //CharacterProperties cp = this.richEditControl1.Document.BeginUpdateCharacters(range1);
            //cp.FontName = "宋体";//打开文件默认文字字体
            //cp.FontSize = 14; //打开文件默认文字大小
            //doc.Sections[0].Page.PaperKind = System.Drawing.Printing.PaperKind.A4;//设置纸张大小           
            //doc.Sections[0].Margins.Left = 280f;//设置左边距
            //doc.Sections[0].Margins.Right = 280f;//设置右边距
            //DocumentPosition pos = doc.Selection.Start;
            //DocumentRange range = doc.CreateRange(pos, 0);
            //ParagraphProperties pp = doc.BeginUpdateParagraphs(range);
            //pp.LineSpacingType = ParagraphLineSpacing.Sesquialteral;//设置段落1.5倍行距                              
            //doc.EndUpdateParagraphs(pp);
            //doc.EndUpdateCharacters(cp);

            ////页眉设置
            //// 第一页 页眉设置
            Section firstSection = doc.Sections[0];
            SubDocument myHeader = firstSection.BeginUpdateFooter(HeaderFooterType.First);
            SetFoot(doc, myHeader, firstSection); 
            //除第一页 之外的页jiao设置
            int pagecount = this.richEditControl1.Document.Sections.Count;
            for (int p = 0; p <= pagecount - 1; p++)
            {
                Section elseSection = doc.Sections[p];
                SubDocument myotherHeader = firstSection.BeginUpdateFooter(HeaderFooterType.Odd);
                SetFoot(doc, myotherHeader, elseSection);
            }

             myHeader = firstSection.BeginUpdateHeader(HeaderFooterType.First);
             SetHead(doc, myHeader, firstSection);
            //除第一页 之外的页jiao设置
          
            for (int p = 0; p <= pagecount - 1; p++)
            {
                Section elseSection = doc.Sections[p];
                SubDocument myotherHeader = firstSection.BeginUpdateHeader(HeaderFooterType.Odd);
                SetHead(doc, myotherHeader, elseSection);
            }
        }
        

        //文档默认格式设置
        private void doc_default(Document doc)
        {
            DocumentRange range1 = richEditControl1.Document.Range;
            CharacterProperties cp = this.richEditControl1.Document.BeginUpdateCharacters(range1);
            cp.FontName = "宋体";//打开文件默认文字字体
            cp.FontSize = 14; //打开文件默认文字大小
            doc.Sections[0].Page.PaperKind = System.Drawing.Printing.PaperKind.A4;//设置纸张大小           
            doc.Sections[0].Margins.Left = 280f;//设置左边距
            doc.Sections[0].Margins.Right = 280f;//设置右边距
            DocumentPosition pos = doc.Selection.Start;
            DocumentRange range = doc.CreateRange(pos, 0);
            ParagraphProperties pp = doc.BeginUpdateParagraphs(range);
            pp.LineSpacingType = ParagraphLineSpacing.Sesquialteral;//设置段落1.5倍行距                              
            doc.EndUpdateParagraphs(pp);
            doc.EndUpdateCharacters(cp);

            //页眉设置
            // 第一页 页眉设置
            Section firstSection = doc.Sections[0];
            SubDocument myHeader = firstSection.BeginUpdateHeader(HeaderFooterType.First);
            get_docheader(doc, myHeader, firstSection);
           
            //document.AppendSection(); //添加一页
            //除第一页 之外的页眉设置
            int pagecount = this.richEditControl1.Document.Sections.Count;
            for (int p = 0; p <= pagecount - 1; p++)
            {
                Section elseSection = doc.Sections[p];
                SubDocument myotherHeader = firstSection.BeginUpdateHeader(HeaderFooterType.Odd);
                get_docheader(doc, myotherHeader, elseSection);
            }
        }

        private void get_docheader(Document document, SubDocument myHeader, Section firstSection)
        {
            DocumentRange range3 = myHeader.InsertText(myHeader.CreatePosition(0), "第");
            Field fld = myHeader.Fields.Create(range3.End, "PAGE");//当前页数
            myHeader.AppendText("页 共");
            Field fld2 = myHeader.Fields.Create(range3.End, "NUMPAGES");//总页数
            myHeader.AppendText("页");

            ParagraphProperties gg = myHeader.BeginUpdateParagraphs(range3);
            gg.Alignment = ParagraphAlignment.Center;//页眉内容居右
            document.EndUpdateParagraphs(gg);
            CharacterProperties cp3 = myHeader.BeginUpdateCharacters(range3);
            cp3.FontName = "宋体";
            cp3.FontSize = 12;
            document.EndUpdateCharacters(cp3);
            myHeader.Fields.Update();
            firstSection.EndUpdateHeader(myHeader);
            firstSection.DifferentFirstPage = true;
        }

        private void SetFoot(Document document, SubDocument myHeader, Section firstSection)
        {
            DocumentRange range3 = myHeader.InsertText(myHeader.CreatePosition(0), "第");
            Field fld = myHeader.Fields.Create(range3.End, "PAGE");//当前页数
            myHeader.AppendText("页 共");
            Field fld2 = myHeader.Fields.Create(range3.End, "NUMPAGES");//总页数
            myHeader.AppendText("页");

            ParagraphProperties gg = myHeader.BeginUpdateParagraphs(range3);
            gg.Alignment = ParagraphAlignment.Center;//页眉内容居右
            document.EndUpdateParagraphs(gg);
            CharacterProperties cp3 = myHeader.BeginUpdateCharacters(range3);
            cp3.FontName = "宋体";
            cp3.FontSize = 12;
            document.EndUpdateCharacters(cp3);
            myHeader.Fields.Update();
            firstSection.EndUpdateFooter(myHeader);
            firstSection.DifferentFirstPage = true;

        }

        private void SetHead(Document document, SubDocument myHeader, Section firstSection)
        {
            DocumentRange range3 = myHeader.InsertText(myHeader.CreatePosition(0), "说明书");
        

            ParagraphProperties gg = myHeader.BeginUpdateParagraphs(range3);
            gg.Alignment = ParagraphAlignment.Center;//页眉内容居右
            document.EndUpdateParagraphs(gg);
            CharacterProperties cp3 = myHeader.BeginUpdateCharacters(range3);
            cp3.FontName = "宋体";
            cp3.FontSize = 12;
            document.EndUpdateCharacters(cp3);
            myHeader.Fields.Update();
            firstSection.EndUpdateHeader(myHeader);
            firstSection.DifferentFirstPage = true;
        }




        ///// <summary>
        ///// 页面设置
        ///// </summary>
        ///// <param name="rch"></param>
        ///// <param name="hadertxt"></param>
        //private void Doc_default(RichEditControl rch, string hadertxt)
        //{
        //    Document doc = rch.Document;
        //    //页眉设置
        //    // 第一页 页眉设置
        //    DocumentRange range1 = rch.Document.Range;
        //    CharacterProperties cp = rch.Document.BeginUpdateCharacters(range1);
        //    cp.FontName = "宋体";//打开文件默认文字字体
        //    cp.FontSize = 16; //打开文件默认文字大小
        //    doc.Sections[0].Page.PaperKind = System.Drawing.Printing.PaperKind.A4;//设置纸张大小           
        //    doc.Sections[0].Margins.Left = 280f;//设置左边距
        //    doc.Sections[0].Margins.Right = 280f;//设置右边距
        //    DocumentPosition pos = doc.Selection.Start;
        //    DocumentRange range = doc.CreateRange(pos, 0);
        //    ParagraphProperties pp = doc.BeginUpdateParagraphs(range);
        //    pp.LineSpacingType = ParagraphLineSpacing.Sesquialteral;//设置段落1.5倍行距                              
        //    doc.EndUpdateParagraphs(pp);
        //    doc.EndUpdateCharacters(cp);

        //    ////页脚设置 
        //    Section firstSection = doc.Sections[0];
        //    SubDocument myHeader = firstSection.BeginUpdateFooter(HeaderFooterType.First);
        //    SetFoot(doc, myHeader, firstSection);
        //    int pagecount = rch.Document.Sections.Count;
        //    for (int p = 0; p <= pagecount - 1; p++)
        //    {
        //        Section elseSection = doc.Sections[p];
        //        SubDocument myotherHeader = firstSection.BeginUpdateFooter(HeaderFooterType.Odd);
        //        SetFoot(doc, myotherHeader, elseSection);
        //    }

        //    //页眉设置
        //    myHeader = firstSection.BeginUpdateHeader(HeaderFooterType.First);
        //    SetHead(doc, myHeader, firstSection, hadertxt);
        //    for (int p = 0; p <= pagecount - 1; p++)
        //    {
        //        Section elseSection = doc.Sections[p];
        //        SubDocument myotherHeader = firstSection.BeginUpdateHeader(HeaderFooterType.Odd);
        //        SetHead(doc, myotherHeader, elseSection, hadertxt);
        //    }
        //}




        //private void SetFoot(Document document, SubDocument myHeader, Section firstSection)
        //{
        //    DocumentRange range3 = myHeader.InsertText(myHeader.CreatePosition(0), " ");

        //    Field fld = myHeader.Fields.Create(range3.End, "PAGE");//当前页数


        //    ParagraphProperties gg = myHeader.BeginUpdateParagraphs(range3);
        //    gg.Alignment = ParagraphAlignment.Center;//页眉内容居右
        //    document.EndUpdateParagraphs(gg);
        //    CharacterProperties cp3 = myHeader.BeginUpdateCharacters(range3);
        //    cp3.FontName = "宋体";
        //    cp3.FontSize = 12;
        //    document.EndUpdateCharacters(cp3);
        //    myHeader.Fields.Update();
        //    firstSection.EndUpdateFooter(myHeader);
        //    firstSection.DifferentFirstPage = true;

        //}

        //private void SetHead(Document document, SubDocument myHeader, Section firstSection, string hadertxt)
        //{
        //    DocumentRange range3 = myHeader.InsertText(myHeader.CreatePosition(0), hadertxt);


        //    ParagraphProperties gg = myHeader.BeginUpdateParagraphs(range3);
        //    gg.Alignment = ParagraphAlignment.Center;//页眉内容居右
        //    document.EndUpdateParagraphs(gg);
        //    CharacterProperties cp3 = myHeader.BeginUpdateCharacters(range3);
        //    cp3.FontName = "宋体";
        //    cp3.FontSize = 12;
        //    document.EndUpdateCharacters(cp3);
        //    myHeader.Fields.Update();
        //    firstSection.EndUpdateHeader(myHeader);
        //    firstSection.DifferentFirstPage = true;
        //}


        private void button2_Click(object sender, EventArgs e)
        {
            string a = "特征";
            List<string> list = new List<string> { "C＃", "标准", "注意" };
            Showy(richEditControl1, a);

        }

        //替换
        private void button1_Click(object sender, EventArgs e)
        {
            Document document = this.richEditControl1.Document;
            document.BeginUpdate();
            document.SelectAll();
            document.ReplaceAll("特征","aBB",SearchOptions.None);  
            document.EndUpdate();
           
          }


        //列表中多个关键字
        private void Showy(RichEditControl re, List<string> strList)
        {
            string str = "";//要高亮的字段
            foreach (string item in strList)
            {
                if (strList.IndexOf(item) == 0)
                    str = item;
                else
                    str += "|" + item;
            }

            DocumentRange[] list = re.Document.FindAll(new System.Text.RegularExpressions.Regex(str));

            for (int i = 0; i < list.Length; i++)
            {
                CharacterProperties cp = re.Document.BeginUpdateCharacters(list[i]);

                cp.ForeColor = Color.Red;
                cp.BackColor = Color.Yellow;

                re.Document.EndUpdateCharacters(cp);

            }
        }

        //针对单个关键字
        private void Showy(RichEditControl re, string strList)
        {
            DocumentRange[] list = re.Document.FindAll(new System.Text.RegularExpressions.Regex(strList));

            for (int i = 0; i < list.Length; i++)
            {
                CharacterProperties cp = re.Document.BeginUpdateCharacters(list[i]);

                cp.ForeColor = Color.Red;
                cp.BackColor = Color.Yellow;
                re.Document.EndUpdateCharacters(cp);
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            //修改绑定
           // richEditBarController1.Control = richEditControl2;
        }
    }
}
